package com.achun.getcouponleague.ui.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.achun.getcouponleague.Bean.CategoriesContent;
import com.achun.getcouponleague.Bean.Red_Packets;
import com.achun.getcouponleague.R;
import com.achun.getcouponleague.base.BaseFragment;
import com.achun.getcouponleague.presenter.impl.Red_packetPresenter;
import com.achun.getcouponleague.presenter.impl.TiketPresenter;
import com.achun.getcouponleague.ui.activity.TiketActivity;
import com.achun.getcouponleague.ui.adapter.Red_packetListAdapter;
import com.achun.getcouponleague.utils.Logutils;
import com.achun.getcouponleague.utils.PresenterManager;
import com.achun.getcouponleague.utils.UIutils;
import com.achun.getcouponleague.view.IRed_packedCallBack;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.List;

import butterknife.BindView;

public class Red_Packet_Fragment extends BaseFragment implements IRed_packedCallBack {
    @BindView(R.id.recycle_view)
    public RecyclerView recyclerView;
    private Red_packetListAdapter red_packetListAdapter;
    private Red_packetPresenter red_packetPresenter;
    @BindView(R.id.refresh_view)
    public TwinklingRefreshLayout twinklingRefreshLayout;
    private TiketPresenter tiketPresenter;

    @Override
    protected void onReTry() {
        red_packetPresenter.getRed_PacketDatas();
    }

    @Override
    public View loadSuccessView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_red_packet, container, false);
    }

    @Override
    protected void initView(View rootview) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        recyclerView.setLayoutManager(gridLayoutManager);
        red_packetListAdapter = new Red_packetListAdapter();
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, int itemPosition, @NonNull RecyclerView parent) {
                int size = UIutils.dip2px(getContext(), 5);
                outRect.right=size;
                outRect.left=size;
                outRect.top=size;
                outRect.bottom=size;
            }
        });
        recyclerView.setAdapter(red_packetListAdapter);
    }
    //处理跳转事件
    private void handleclick(Red_Packets.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean item){
        String coupon_share_url = "https:"+item.getCoupon_share_url();
        String title = item.getTitle();
        String cover = item.getPict_url();
        tiketPresenter.createTiketCode(coupon_share_url,title,cover);
        Intent intent = new Intent(getContext(), TiketActivity.class);
        startActivity(intent);
    }
    @Override
    protected void initListener() {
        //item点击事件
        red_packetListAdapter.setOnItemClickListener(new Red_packetListAdapter.OnItemClickListener() {
            @Override
            public void OnItemCLick(Red_Packets.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean bean) {
                handleclick(bean);
            }
        });
        twinklingRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                red_packetPresenter.getLoadMoreRed_Packet(true);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                red_packetPresenter.getLoadMoreRed_Packet(false);
            }
        });
    }

    @Override
    protected void initPresenter() {
        red_packetPresenter = PresenterManager.getInstance().getRed_packetPresenter();
        tiketPresenter = PresenterManager.getInstance().getTiketPresenter();
        red_packetPresenter.registerCallback(this);
        red_packetPresenter.getRed_PacketDatas();
    }

    @Override
    public void onLoading() {
        setViewStatus(Status.LOADING);
    }

    @Override
    public void onError() {
        setViewStatus(Status.ERROR);
    }

    @Override
    public void onEmpty() {
        setViewStatus(Status.EMPTY);
    }

    @Override
    public void OnLoadedRed_packedDatas(Red_Packets red_packets) {
        setViewStatus(Status.SUCCESS);
        List<Red_Packets.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> map_data = red_packets.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data();

        red_packetListAdapter.setDatas(map_data);
    }

    @Override
    public void OnloadMoreRed_packedDatas(Red_Packets red_packets,boolean isrefresh) {
        List<Red_Packets.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> map_data = red_packets.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data();
        if (isrefresh){
            twinklingRefreshLayout.finishRefreshing();
            red_packetListAdapter.setDatas(map_data);
            Toast.makeText(getContext(), "为您更新了" + map_data.size() + "条数据", Toast.LENGTH_SHORT).show();
        }else {
            twinklingRefreshLayout.finishLoadmore();
            red_packetListAdapter.addDatas(map_data);
            Toast.makeText(getContext(), "为您加载了" + map_data.size() + "条数据", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void OnloadMoreError() {
        twinklingRefreshLayout.onFinishLoadMore();
        Toast.makeText(getContext(),"网络错误，请稍后重试",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void OnloadMoreEmpty() {

    }
}